package com.cj.user.service.thread;

import lombok.SneakyThrows;

import java.util.concurrent.*;

/**
 * @Author cheng jun
 * @Description: 线程的复用，一个线程执行完任务后，这个线程继续执行新的任务，即不用去线程池拿新的线程
 * @Date 2021/7/17 21:17
 */
public class ThreadPoolDemo implements Runnable {

    private final int i;

    public ThreadPoolDemo(int i) {
        this.i = i;
    }

    public static void main(String[] args) {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                2, 3, 60,
                TimeUnit.SECONDS, new ArrayBlockingQueue<>(5), Executors.defaultThreadFactory());
        for (int i = 0; i < 9; i++) {
            threadPoolExecutor.execute(new ThreadPoolDemo(i));
        }
        threadPoolExecutor.shutdownNow();
    }

    @SneakyThrows
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + "...." + i);
    }
}
